package com.glela.api.upload.qiniu.http;

import java.io.UnsupportedEncodingException;

import org.apache.commons.httpclient.Header;
import org.apache.commons.lang3.StringUtils;

public class HttpResponse
{

	/**
	 * 返回中的Header信息
	 */
	private Header[] responseHeaders;

	/**
	 * String类型的result
	 */
	private String stringResult;

	/**
	 * HTTP响应码
	 */
	private int httpCode;

	/**
	 * btye类型的result
	 */
	private byte[] byteResult;

	private String reason;

	public byte[] getByteResult() throws UnsupportedEncodingException
	{
		if (byteResult != null) {
			return byteResult;
		}
		if (stringResult != null) {
			
			return stringResult.getBytes("utf-8");
		}
		return null;
	}

	public String getHeader(String key)
	{
		if (responseHeaders != null) {
			for (Header header : responseHeaders) {
				if (StringUtils.equalsIgnoreCase(key, header.getName())) {
					return header.getValue();
				}
			}
		}
		return null;
	}

	public int getHttpCode()
	{
		return httpCode;
	}

	public String getReason()
	{
		return reason;
	}

	public Header[] getResponseHeaders()
	{
		return responseHeaders;
	}

	public String getStringResult()
	{
		return stringResult;
	}

	public boolean isSucced()
	{
		return httpCode == 200;
	}

	public void setByteResult(byte[] byteResult)
	{
		this.byteResult = byteResult;
	}

	public void setHttpCode(int httpCode)
	{
		this.httpCode = httpCode;
	}

	public void setReason(String reason)
	{
		this.reason = reason;
	}

	public void setResponseHeaders(Header[] responseHeaders)
	{
		this.responseHeaders = responseHeaders;
	}

	public void setStringResult(String stringResult)
	{
		this.stringResult = stringResult;
	}

}
